package com.google.unizone.server;

import com.google.unizone.timetable.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.restfb.Parameter;

public class Utils {
	public static String backToHomeToString() {
		return "<a href = \"index.jsp\">Back to Homepage</a>";
	}
	
	public static List<String> parseCourseIDs(String schedule){
	//public static String parseCourseIDs(String schedule){
		String temp = "";
		List<String> CourseIDs = new ArrayList<String>();
		Pattern p = Pattern.compile("\\d{4}-\\d{4}-\\d{2}");
		Matcher matcher;
		matcher = p.matcher(schedule);
		while (matcher.find()) {
			temp += matcher.group();
			CourseIDs.add(matcher.group());
		}
		//return temp;
		return CourseIDs;
	}
	
	/*
	@Test
	public void f()
	{
		List<String> uids = new ArrayList<String>();
		uids.add("0368-3500-10");
		uids.add("0311-3500-10");
		uids.add("0368-3500-12");
		CreateSchedule(uids, new Student());
	}*/
	
	public static List<String> CreateSchedule(List<String> Uids, Student student) {
		List<String> badUids = new ArrayList<String>();
		Set<String> courses = student.getCourseIDList();
		List<Course> cs = new ArrayList<Course>();
		
		Course course;
		for (String courseID : courses)
			cs.add(DB_Logic.getCourse(courseID));
		for (Course c : cs)
		{
			DB_Logic.removeStudentFromCourse(student, c);
		}
		for (Course c : cs)
		{
			DB_Logic.removeCourseFromStudent(student, c);
		}
		for (String string : Uids) {
			
			course = DB_Logic.getCourse(string);
			if (course == null){
				//crreate course
				SearchKeys searchKeys = new SearchKeys(string, 2011);
				try {
					course = new CourseFinder(searchKeys).getCourse();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(course != null && !course.getCourseName().equals("exercise"))
				{
					course.addStudent(student);
					DB_Logic.createCourse(course);
					student.addCourseID(string);
				}
				//System.out.println(course.getCourseName());
				//System.out.println(course.toString());
			}
			else
			{
				DB_Logic.addStudentToCourse(student, course);
				//course.addStudent(student);
				student.addCourseID(string);
			}
		}
		return badUids;
	}
	
	public static String rapHTMLTag(String input, String tag) {
		return "<" + tag + ">" + input + "</" + tag +">";
	}
	
	
	public static String appToString (String page){
		String pic;
		if (page == "register")
			pic = "registerPic.jpg";
		else if (page == "homepage")
			pic = "homePagePic.jpg";
		else if (page == "course")
			pic = "coursePic.jpg";
		else if (page == "friend")
			pic = "friendPic.jpg";
		else return "";
		return "<h4>alpha version</h4><A href=\"index.jsp\"><img id=\"appTitle\" src=\"" + pic +"\"></A>";
	}
	
	public static String appToString ()
	{
		return "";
	}
	
	public static String[][]  scheduleToArray (Student student) {
		List<IShiur> schedule = DB_Logic.getSchedule(student);
		String[][] SheduleArray =  new String[13][7];
		int day, StartTime, EndTime, Length;
		for (IShiur iShiur : schedule) {
			day = iShiur.getDay();
			StartTime = iShiur.getBeginTime().getHours();
			EndTime = iShiur.getEndTime().getHours();
			Length = EndTime - StartTime;
			for (int i=0;i<Length;i++){
				if (SheduleArray[StartTime-7+i][day] == null){
					SheduleArray[StartTime-7+i][day] =iShiur.nameToString();
				}
				else if(SheduleArray[StartTime-7+i][day].startsWith("overlapping!!!")) {
					SheduleArray[StartTime-7+i][day] = SheduleArray[StartTime-7+i][day] + ",<br>" + iShiur.nameToString(); 
				}
				else {
					SheduleArray[StartTime-7+i][day] = "overlapping!!!<br>" + SheduleArray[StartTime-7+i][day] + ",<br>" + iShiur.nameToString();
				}
			}

		}
		return SheduleArray;
	}
	
	public static String  scheduleToString (Student student) {
		String[][] scheduleArray  =scheduleToArray(student);
		String iShiurName = "";
		int columnWidth = 20;
		int shiurWidth;
		int Spaces2Add;
		scheduleArray[0][1] = "Sunday";
		scheduleArray[0][2] = "Monday";
		scheduleArray[0][3] = "Tuesday";
		scheduleArray[0][4] = "Wednesday";
		scheduleArray[0][5] = "Thursday";
		scheduleArray[0][6] = "Friday";
		scheduleArray[1][0] = "8:00";
		scheduleArray[2][0] = "9:00";
		scheduleArray[3][0] = "10:00";
		scheduleArray[4][0] = "11:00";
		scheduleArray[5][0] = "12:00";
		scheduleArray[6][0] = "13:00";
		scheduleArray[7][0] = "14:00";
		scheduleArray[8][0] = "15:00";
		scheduleArray[9][0] = "16:00";
		scheduleArray[10][0] = "17:00";
		scheduleArray[11][0] = "18:00";
		scheduleArray[12][0] = "19:00";

		String output = "";
		output += "<table id=\"schedule\"";
		for (int i = 0;i<scheduleArray.length;i++){
			output += "<tr>";
			for (int j=0;j<scheduleArray[i].length;j++){
				if(i == 0 || j == 0)
					output += "<td id=\"header\">";
				else
					output += "<td>";
				if (scheduleArray[i][j] != null){
					if (scheduleArray[i][j].length() >= 10){
						output += "<a rel=\"htmltooltip\">" + scheduleArray[i][j].substring(0, 10) + "</a>";
						output += "<div class=\"htmltooltip\">" + scheduleArray[i][j] + "</div>";
					}
					else{
						output += scheduleArray[i][j];
					}
				}
				output += "</td>";
			}
			output += "</tr>";
		}
		output += "</table>";
		return output;
	}
	
	public static Set<String> FBFriendsToSet(List<Student> friends){
		Set<String> fixedFriends = new HashSet<String>();
		for (Student student: friends){
			fixedFriends.add(student.getFacebookID());
		}
		return fixedFriends;
	}

	public static boolean areFriends(Student meStudent, String otherStudentID){
		Set<String> friends = meStudent.getFBFriends();
		if (friends.contains(otherStudentID)){
			return true;
		}
		else {
			return false;
		}
	}	
	public static String toolbarToString(String FBid){
		return "<A href = \"register.jsp?FBid=" + FBid + "\">Update Schedule</A> |";
	}
}

